找传奇、传世资源到传世资源站!

ASP.NET文件的上传下载

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboard
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;namespace WebApplication1{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void BtnUpload_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { string fileName = FileUpload1.FileName;//获取上传的文件名 this.TextBox1.Text = fileName; //获取上传文件的后缀名 String filefix = fileName.Substring(fileName.LastIndexOf('.') 1).ToLower(); if (filefix!="png"&&filefix!="jpg"&&filefix!="jpeg"&&filefix!="gif") { FileUpload1.SaveAs(Server.MapPath(".") "//UploadPic//" fileName); this.ImgPic.ImageUrl = ""; this.lblMessage.Text = "文件上传成功!"; } else { FileUpload1.SaveAs(Server.MapPath(".") "//UploadPic//" fileName); this.ImgPic.ImageUrl = "~/UploadPic/" fileName; this.lblMessage.Text = "图片上传成功!"; } } } protected void btnDownload_Click(object sender, EventArgs e) { string name = this.TextBox1.Text; //MessageBox.Show(name); string filename = Server.MapPath(".") "/UploadPic/" name; FileInfo fileinfo = new FileInfo(filename); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Disposition", "attachment;filename=" Server.UrlEncode(fileinfo.Name)); Response.AddHeader("Content-Length", fileinfo.Length.ToString()); Response.ContentType = "application/x-bittorrent"; Response.WriteFile(fileinfo.FullName); Response.End(); //Response.ContentType = "application/x-zip-compressed"; //Response.AddHeader("Content-Disposition", "attachment;filename=" name); //Response.TransmitFile(filename); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复